<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
  @import './style.less';
  @import '../components/style/comments.less';
</style>

<template>
  <div>
    <Row :gutter="10">
      <Card>
        <div class="table-head">
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <Row class="margin-top-10">
          <div class="wordStyle-list">
            <div class="list" v-for="el in data">
              <div class="user">
                <img class="cursor" :src="el.user.thumb" alt="头像" v-if="el.user && el.user.thumb"
                     @click="searchUserInfo(el.user.id)">
                <img class="cursor" src="/vendors/images/person.png" alt="头像" v-if="!(el.user && el.user.thumb)"
                     @click="searchUserInfo(el.user.id)">
                <!--<div class="img-div" v-if="!(el.user && el.user.thumb)" v-text="el.user.name.substring(0,1)"></div>-->
                <div class="name" v-text="el.user.name"></div>
                <div class="name" v-text="el.department.name"></div>
                <div class="name" v-text="el.status.title"></div>
                <div class="read">阅读 {{el.clicks ? el.clicks : 0}}</div>
              </div>
              <div class="title cursor" v-text="el.title" @click="show(el)"></div>
              <div class="content cursor" v-html="el.content" @click="show(el)"></div>
              <div class="action">
                <div class="support" @click="supportFun(el.id, el.subject.praise.is_support)">
                  <Icon type="ios-thumbs-up"
                        v-if="el.subject.praise.is_support.toString() == 'true' && el.subject.praise.is_support.toString() != ''"/>
                  <Icon type="ios-thumbs-up-outline"
                        v-if="el.subject.praise.is_support.toString() == 'false' || el.subject.praise.is_support.toString() == ''"/>
                  {{el.subject.praise.support}}
                </div>
                <div class="against" @click="againstFun(el.id, el.subject.praise.is_support)">
                  <Icon type="ios-thumbs-down"
                        v-if="el.subject.praise.is_support.toString() == 'false' && el.subject.praise.is_support.toString() != ''"/>
                  <Icon type="ios-thumbs-down-outline"
                        v-if="el.subject.praise.is_support.toString() == 'true' || el.subject.praise.is_support.toString() == ''"/>
                  {{el.subject.praise.against}}
                </div>
                <div class="comments" @click="commentsFun(el.id)">
                  <Icon type="ios-text"/>
                  <span
                      v-if="el.show_comments_list != true">评论（{{el.subject.reply.reply_number > 0 ? el.subject.reply.reply_number : 0}}）</span>
                  <span v-if="el.show_comments_list == true">收起评论</span>
                </div>
                <div class="collection" @click="collectionFun(el.id, el.subject.collection.is_collection)">
                  <Icon type="ios-star" v-if="el.subject.collection.is_collection.toString() == 'true'"/>
                  <Icon type="ios-star-outline" v-if="el.subject.collection.is_collection.toString() == 'false'"/>
                  收藏
                </div>
                <div class="delete" @click="remove(el.id)" v-if="uid == el.user.id">删除</div>
                <div class="delete" v-if="uid == el.user.id && (el.status.id == 1 || el.status.id == 4)"
                     @click="update(el.id)">编辑
                </div>
              </div>
              <div class="comments-list" v-if="el.show_comments_list == true">
                <comments-list
                    reply_type="work_style"
                    :reply_type_id="el.id"
                ></comments-list>
              </div>
              <div class="border"></div>
            </div>
          </div>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      <!--详情-->
      <Drawer :closable="false" :width="DrawerWidth" v-model="showCurrentTableData" class="Drawer-body">
        <div class="Drawer_pStyle">
          <span></span>
          <span class="del" @click="asyncOK"><Icon type="md-close"/></span>
        </div>
        <div class="body padding-16">
          <div class="reply_list">
            <div class="reply_list_second">
              <div class="user">
                <table class="table" cellpadding="0" cellspacing="0">
                  <tr>
                    <td class="td1">
                      <img class="cursor" :src="dataDetail.user.thumb" alt="头像"
                           v-if="dataDetail.user && dataDetail.user.thumb" @click="searchUserInfo(dataDetail.user.id)">
                      <img class="cursor" src="/vendors/images/person.png" alt="头像"
                           v-if="!(dataDetail.user && dataDetail.user.thumb)"
                           @click="searchUserInfo(dataDetail.user.id)">
                      <!--<div class="img-div"
                           v-if="!(dataDetail.user && dataDetail.user.thumb) && (dataDetail.user && dataDetail.user.name)"
                           v-text="dataDetail.user.name.substring(0,1)"></div>-->
                    </td>
                    <td class="td3">
                      <div class="name" v-if="dataDetail.user && dataDetail.user.name"
                           v-text="dataDetail.user.name"></div>
                    </td>
                    <td class="td4">
                      <div class="name" v-if="dataDetail.department && dataDetail.department.name"
                           v-text="dataDetail.department.name"></div>
                    </td>
                    <td class="td3">
                      <div class="name" v-if="dataDetail.status && dataDetail.status.title"
                           v-text="dataDetail.status.title"></div>
                    </td>
                    <td>
                      <div class="time">阅读 {{dataDetail.clicks ? dataDetail.clicks : 0}}</div>
                    </td>
                  </tr>
                </table>
              </div>
              <div class="title detail-title" v-text="dataDetail.title"></div>
              <div class="content ck-editor-content" v-html="dataDetail.content"></div>
              <div class="action"
                   v-if="dataDetail.subject && dataDetail.subject.praise">
                <div class="support" @click="supportFun(dataDetail.id, dataDetail.subject.praise.is_support)">
                  <Icon type="ios-thumbs-up"
                        v-if="dataDetail.subject.praise.is_support.toString() == 'true' && dataDetail.subject.praise.is_support.toString() != ''"/>
                  <Icon type="ios-thumbs-up-outline"
                        v-if="dataDetail.subject.praise.is_support.toString() == 'false' || dataDetail.subject.praise.is_support.toString() == ''"/>
                  {{dataDetail.subject.praise.support}}
                </div>
                <div class="against" @click="againstFun(dataDetail.id, dataDetail.subject.praise.is_support)">
                  <Icon type="ios-thumbs-down"
                        v-if="dataDetail.subject.praise.is_support.toString() == 'false' && dataDetail.subject.praise.is_support.toString() != ''"/>
                  <Icon type="ios-thumbs-down-outline"
                        v-if="dataDetail.subject.praise.is_support.toString() == 'true' || dataDetail.subject.praise.is_support.toString() == ''"/>
                  {{dataDetail.subject.praise.against}}
                </div>
                <div class="comments" @click="commentsFun(dataDetail.id)">
                  <Icon type="ios-text"/>
                  <span
                      v-if="dataDetail.show_comments_list != true">评论（{{dataDetail.subject.reply.reply_number > 0 ? dataDetail.subject.reply.reply_number : 0}}）</span>
                  <span v-if="dataDetail.show_comments_list == true">收起评论</span>
                </div>
                <div class="collection"
                     @click="collectionFun(dataDetail.id, dataDetail.subject.collection.is_collection)">
                  <Icon type="ios-star" v-if="dataDetail.subject.collection.is_collection.toString() == 'true'"/>
                  <Icon type="ios-star-outline"
                        v-if="dataDetail.subject.collection.is_collection.toString() == 'false'"/>
                  收藏
                </div>
                <div class="delete" @click="remove(dataDetail.id)" v-if="uid == dataDetail.user.id">删除</div>
                <div class="delete"
                     v-if="uid == dataDetail.user.id && (dataDetail.status.id == 1 || dataDetail.status.id == 4)"
                     @click="update(dataDetail.id)">编辑
                </div>
              </div>
              <div class="comments-list" v-if="dataDetail.show_comments_list == true">
                <comments-list
                    reply_type="work_style"
                    :reply_type_id="dataDetail.id"
                ></comments-list>
              </div>
            </div>
          </div>
        </div>
        <div class="footer" :style="{width: DrawerWidth - 32 + 'px'}">

        </div>
      </Drawer>
      <!--编辑-->
      <Drawer :closable="false" :width="WindowWidth" v-model="addCurrentTableData" class="Drawer-body">
        <div class="Drawer_pStyle">
          <span>编辑</span>
          <span class="del" @click="hideAddModal"><Icon type="md-close"/></span>
        </div>
        <div class="body">
          <table class="add-table" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td class="left paddingTop22" valign="top" align="right">
                <Form label-position="top" class="from-content">
                  <FormItem label="">
                    <Input v-model="addData.title" placeholder="请输入标题"></Input>
                  </FormItem>
                  <FormItem label="">
                    <!--<textarea name="content" id="content"></textarea>-->
                    <div id="content" class="ck-editor-content" :style="{minHeight: WindowHeight - 252 + 'px'}"></div>
                    <!--<div id="toolbar-container"></div>
                    <div id="editor" class="editor-content"></div>-->
                  </FormItem>
                </Form>
              </td>
              <td class="right" valign="top" :style="{width: WindowWidth > 1440 ? WindowWidth*0.3 + 'px' : '288px'}">
                <Form label-position="top">
                  <FormItem label="发布部门">
                    <Select v-model="addData.department" @on-change="onChangeTypeSelect" :transfer="true"
                            style="width:100%;">
                      <Option v-for="item in departmentArr" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                  <FormItem label="审批人">
                    <Select v-model="addData.approvers" :transfer="true" style="width:100%;">
                      <Option v-for="item in approversArr" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                  </FormItem>
                </Form>
              </td>
            </tr>
          </table>
        </div>
        <div class="footer" :style="{width: WindowWidth - 32 + 'px'}">
          <Button type="text" @click="hideAddModal">取消</Button>
          <Button type="info" :loading="saveLoading" @click="save(status)">保存</Button>
          <Button type="primary" :loading="saveLoading" @click="saveAndApproval()">保存并提交审批</Button>
        </div>
      </Drawer>
      <!--收藏-->
      <Modal v-model="show_collection_modal" :width="600"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">收藏</h3>
        <div class="collection-div">
          <CheckboxGroup v-model="Checkbox_collection_group" class="Checkbox">
            <Checkbox v-for="(el,index) in collection_group_data" :key="index" :label="el.id">
              <span v-text="el.name"></span>
            </Checkbox>
          </CheckboxGroup>
        </div>
        <div slot="footer">
          <Button type="text" @click="hidecollectionModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="savecollection()">确定</Button>
        </div>
      </Modal>
      <!--人员详情-->
      <user-info
          :user_id="userInfo_id"
          @userInfoBackFun="userInfoBackFun"
      ></user-info>
    </Row>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Ckeditor from '../../libs/mod.ckeditor';
  import Modal from '../../libs/mod.Modal';
  import commentsList from '../components/comments-list.vue';
  import userAvatar from '../components/user-avatar.vue';
  import userInfo from '../components/user-info.vue';
  export default {
    components: {
      commentsList,
      userAvatar,
      userInfo
    },
    data () {
      return {
        uid: Util.toJson(localStorage.getItem('userInfo')).id,
        total: 0,
        page: 1,
        size: 10,
        id: '',
        show_users: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'title',
            title: '标题',
          },
          {
            key: 'user',
            title: '发布人',
            render: (h, params) => {
              var name = params.row.user ? params.row.user.name : '';
              return h('div', [
                h('span', name)
              ]);
            }
          },
          {
            key: 'department',
            title: '部门',
            render: (h, params) => {
              var name = params.row.department ? params.row.department.name : '';
              return h('div', [
                h('span', name)
              ]);
            }
          },
          {
            key: 'create_time',
            title: '创建时间',
          },
          {
            key: 'status',
            title: '状态',
            render: (h, params) => {
              var title = params.row.status ? params.row.status.title : '';
              return h('div', [
                h('span', title)
              ]);
            }
          },
          {
            title: 'Action',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              // 1 草稿,2 审批中,3 审批通过,4 审批驳回
              var status = params.row.status ? params.row.status.id : '';
              var disabled = true;
              if (status == 1 || status == 4) {
                disabled = false;
              }
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small',
                    disabled: disabled,
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: e => {
                      e.stopPropagation();
                      var data = params.row;
                      this.update(data, params.index);
                    }
                  }
                }, '修改')
              ]);
            }
          }
        ],
        data: {},
        dataDetail: {},
        initTable: [],
        addCurrentTableData: false,
        saveLoading: false,
        ajaxType: 'add',
        addData: {
          title: '',
          department: '',
          approvers: '',
          content: '',
        },
        selectionArr: [],
        departmentArr: [],
        approversArr: [],
        editor: null,
        status: 1,
        comePageTime: 0,// 进入页面的时间戳
        support: false,
        against: false,
        supportNum: 0,
        againstNum: 0,
        list_tid: '',//收藏内容id
        list_is_collection: '',//是否收藏了
        show_collection_modal: false,
        collection_group_data: [],
        Radio_collection_group: '',
        Checkbox_collection_group: [],
        DrawerWidth: Util.constant.DrawerWidth,// 抽屉的宽度
        WindowWidth: Util.constant.WindowWidth,// 窗口的宽度
        WindowHeight: Util.constant.WindowHeight,// 窗口的高度
        userInfo_id: '',// 查看用户详情的用户id
      };
    },
    beforeMount: function () {
      var _this = this;
      var comePageTime = _this.comePageTime;
      var timestamp = new Date().getTime();
      _this.comePageTime = timestamp;
    },
    beforeDestroy: function () {
      var _this = this;
      var comePageTime = _this.comePageTime;
      var timestamp = new Date().getTime();
      var type = Util.constant.statistical_type.work_style.id;
      var seconds = Math.ceil((parseInt(timestamp) - parseInt(comePageTime)) / 1000);
      Util.statistical(_this, type, seconds);//保存数据
    },
    created: function () {
      var _this = this;
      // 收藏夹跳转过来的
      var collection_list_str = sessionStorage.getItem('collection_list');
      if (collection_list_str && collection_list_str != '') {
        var collection_list_obj = Util.toJson(collection_list_str);
        var id = collection_list_obj.id || '';
        var type = collection_list_obj.type || '';

        _this.show(collection_list_obj);

        sessionStorage.removeItem('collection_list');
      }
    },
    mounted () {
      this.getData();
    },
    methods: {
      // 查看用户详情
      searchUserInfo: function (id) {
        var _this = this;
        console.log('===========', id);
        _this.userInfo_id = id;
      },
      userInfoBackFun: function (id) {
        var _this = this;
        _this.userInfo_id = id;
      },
      // 支持
      supportFun: function (tid, is_support) {
        var _this = this;
        is_support = is_support.toString();
        if (is_support == 'true' && is_support != '') {//已经支持 -> 取消支持
          _this.supportSave(tid, 'work_style', '', true, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.support = _this.data[key].subject.praise.support - 1 >= 0 ? _this.data[key].subject.praise.support - 1 : 0;
              _this.data[key].subject.praise.is_support = '';
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.support = _this.dataDetail.subject.praise.support - 1 >= 0 ? _this.dataDetail.subject.praise.support - 1 : 0;
                _this.dataDetail.subject.praise.is_support = '';
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        } else if (is_support == 'false' && is_support != '') {//已经反对 -> 变为支持
          _this.supportSave(tid, 'work_style', true, false, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.support = _this.data[key].subject.praise.support + 1;
              _this.data[key].subject.praise.against = _this.data[key].subject.praise.against - 1 >= 0 ? _this.data[key].subject.praise.against - 1 : 0;
              _this.data[key].subject.praise.is_support = true;
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.support = _this.dataDetail.subject.praise.support + 1;
                _this.dataDetail.subject.praise.against = _this.dataDetail.subject.praise.against - 1 >= 0 ? _this.dataDetail.subject.praise.against - 1 : 0;
                _this.dataDetail.subject.praise.is_support = true;
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        } else if (is_support == '' && is_support != 'false' && is_support != 'true') {//未支持未反对 -> 支持
          _this.supportSave(tid, 'work_style', true, false, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.support = _this.data[key].subject.praise.support + 1;
              _this.data[key].subject.praise.is_support = true;
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.support = _this.dataDetail.subject.praise.support + 1;
                _this.dataDetail.subject.praise.is_support = true;
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        }
      },
      // 反对
      againstFun: function (tid, is_support) {
        var _this = this;
        is_support = is_support.toString();
        if (is_support == 'true' && is_support != '') {//已经支持 -> 变为反对
          _this.supportSave(tid, 'work_style', false, false, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.support = _this.data[key].subject.praise.support - 1 >= 0 ? _this.data[key].subject.praise.support - 1 : 0;
              _this.data[key].subject.praise.against = _this.data[key].subject.praise.against + 1;
              _this.data[key].subject.praise.is_support = false;
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.support = _this.dataDetail.subject.praise.support - 1 >= 0 ? _this.dataDetail.subject.praise.support - 1 : 0;
                _this.dataDetail.subject.praise.against = _this.dataDetail.subject.praise.against + 1;
                _this.dataDetail.subject.praise.is_support = false;
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        } else if (is_support == 'false' && is_support != '') {//已经反对 -> 取消反对
          _this.supportSave(tid, 'work_style', '', true, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.against = _this.data[key].subject.praise.against - 1 >= 0 ? _this.data[key].subject.praise.against - 1 : 0;
              _this.data[key].subject.praise.is_support = '';
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.against = _this.dataDetail.subject.praise.against - 1 >= 0 ? _this.dataDetail.subject.praise.against - 1 : 0;
                _this.dataDetail.subject.praise.is_support = '';
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        } else if (is_support == '' && is_support != 'false' && is_support != 'true') {//未支持未反对 -> 反对
          _this.supportSave(tid, 'work_style', false, false, function (res) {
            if (res.result == 0) {
              var key = 'key_' + tid;
              _this.data[key].subject.praise.against = _this.data[key].subject.praise.against + 1;
              _this.data[key].subject.praise.is_support = false;
              if (_this.showCurrentTableData == true) {
                _this.dataDetail.subject.praise.against = _this.dataDetail.subject.praise.against + 1;
                _this.dataDetail.subject.praise.is_support = false;
              }
            } else {
              _this.$Message.error('保存信息失败!');
            }
          });
        }
      },
      // 支持反对保存
      supportSave: function (tid, type, is_support, is_cancel, _successFun) {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          tid: tid,
          type: type,
          is_support: is_support,//"true 支持 false 反对"
          is_cancel: is_cancel,//"true 取消支持或取消反对"
        };
        var params = {
          url: '/support/save',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
      },
      // 收藏
      collectionFun: function (tid, is_collection) {
        var _this = this;
        _this.list_tid = tid;
        _this.list_is_collection = is_collection;
        is_collection = is_collection.toString();
        if (is_collection == 'true') {//已经收藏 -> 取消收藏
          _this.get_collection_group();
        } else if (is_collection == 'false') {//未收藏 -> 加入收藏
          _this.get_collection_group();
        }
      },
      //收藏夹列表
      get_collection_group: function () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          tid: _this.list_tid,
          type: 'work_style',
        };
        var params = {
          url: '/collection/group/listForTid',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            var Checkbox_collection_group = [];
            for (var k in _data) {
              if (!_data[k]) {
                continue;
              }
              if (_data[k].is_collection == 1) {
                Checkbox_collection_group.push(_data[k].id);
              }
            }
            _this.Checkbox_collection_group = Checkbox_collection_group;
            _this.collection_group_data = _data;
            _this.show_collection_modal = true;
          } else {
            _this.$Message.error('查询收藏夹信息失败!');
          }
        }
      },
      // 隐藏收藏夹
      hidecollectionModal: function () {
        var _this = this;
        _this.show_collection_modal = false;
      },
      // 保存收藏
      savecollection: function () {
        var _this = this;
        var Checkbox_collection_group = _this.Checkbox_collection_group;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          tid: _this.list_tid,
          type: 'work_style',
          gids: Checkbox_collection_group,
        };
        var params = {
          url: '/collection/update',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var is_collection = false;
            if (Checkbox_collection_group.length > 0) {
              is_collection = true;
            }
            var key = 'key_' + _this.list_tid;
            _this.data[key].subject.collection.is_collection = is_collection;
            if (_this.showCurrentTableData == true) {
              _this.dataDetail.subject.collection.is_collection = is_collection;
            }
            _this.show_collection_modal = false;
          } else {
            _this.$Message.error('收藏失败!');
          }
        }
      },
      // 取消收藏
      del_collection: function () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          tid: _this.list_tid,
          type: 'work_style',
        };
        var params = {
          url: '/collection/delete',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var key = 'key_' + _this.list_tid;
            _this.data[key].subject.collection.is_collection = false;
          } else {
            _this.$Message.error('取消收藏失败!');
          }
        }
      },
      // 评论
      commentsFun: function (id) {
        var _this = this;
        var key = 'key_' + id;
        if (_this.showCurrentTableData == true) {
          if (_this.dataDetail.show_comments_list) {
            _this.dataDetail.show_comments_list = false;
          } else {
            _this.dataDetail.show_comments_list = true;
          }
        } else {
          if (_this.data[key].show_comments_list) {
            _this.data[key].show_comments_list = false;
          } else {
            _this.data[key].show_comments_list = true;
          }
        }
      },
      // 工作风采列表
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
//          department_id: 4
        };
        var params = {
          url: '/sd/work-style/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            var dataObj = {};
            for (var k in _data) {
              if (!_data[k]) {
                continue;
              }
              var id = _data[k].id;
              var key = 'key_' + id;
              if (!dataObj[key]) {
                dataObj[key] = {};
              }
              _data[k].show_comments_list = false;
              dataObj[key] = _data[k];
            }
            _this.data = dataObj;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        console.log('page', page);
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        _this.show(data);
      },
      show: function (data) {
        var _this = this;
        var id = data.id;
        _this.id = id;

        var data = {
          id: _this.id,
        };
        var params = {
          url: '/sd/work-style/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _data.show_comments_list = false;
            _this.dataDetail = _data;
            _this.showCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 获取发布部门
      getDepartmentArr: function () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
        };
        var params = {
          url: '/department/getAllMyDepartment',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.departmentArr = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      // 选择部门触发
      onChangeTypeSelect: function (val) {
        var _this = this;
        _this.getApproversArr(val);
      },
      // 获取审批人
      getApproversArr: function (did) {
        var _this = this;
        if (!(did && did != '')) {
          return false;
        }
        _this.approversArr = [];
        var data = {
          department_id: did,
          position: [2],
        };
        var params = {
          url: '/approval/getDepartmentLeader',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.approversArr = _data;
            if (_data.length == 0) {
              _this.$Message.error('该部门暂无审批人，请选择其他部门!');
            }
          } else {
            _this.$Message.error('查询审批人信息失败!');
          }
        }
      },
      // 富文本
      initCkeditor () {
        var _this = this;
        Ckeditor.initCkeditor(_this, 'content', function (editor) {
          _this.editor = editor;
          _this.editor.setData(_this.addData.content);
        });
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        var title = _this.addData.title;
        var content = _this.editor ? _this.editor.getData() : '';
        if (title == '' && (content == '' || content == '<p>&nbsp;</p>')) {
          _this.addCurrentTableData = false;
        } else {
          Modal.confirm(_this, {content: '取消编辑内容将丢失，请确认？'}, function () {
            _this.addCurrentTableData = false;
            if (_this.editor) {
              _this.editor.setData('');
            }
          }, function () {
          });
        }
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.status = 1;
        _this.addData.title = '';
        _this.addData.department = '';
        _this.addData.approvers = '';
        _this.addData.content = '';
        _this.departmentArr = [];
        _this.approversArr = [];
        _this.getDepartmentArr();
        _this.ajaxType = 'add';
        _this.addCurrentTableData = true;
        if (!_this.editor) {
          _this.initCkeditor();
        } else {
          _this.editor.setData('');
        }
      },
      // 保存并提交审批
      saveAndApproval: function () {
        var _this = this;
        _this.save(2);//提交审批
      },
      // 保存数据
      save: function (status) {
        var _this = this;
        var title = _this.addData.title;
        var department = _this.addData.department;
        var approvers = _this.addData.approvers;
//        var content = _this.addData.content;
        var content = _this.editor ? _this.editor.getData() : '';
        if (title == '') {
          _this.$Message.error('请输入标题!');
          return false;
        }
        if (department == '') {
          _this.$Message.error('请选择部门!');
          return false;
        }
        if (approvers == '') {
          _this.$Message.error('请选择审批人!');
          return false;
        }
        if (content == '' || content == '<p>&nbsp;</p>') {
          _this.$Message.error('请输入内容!');
          return false;
        }
        if (!status) { // 1 草稿,2 审批中,3 审批通过,4 审批驳回
          status = 1;
        }
        var data = {
          id: _this.id,
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          title: title,
          content: content,
          department_id: department,
          approvers: [approvers],
          status: status,
        };
        var url = '/sd/work-style/add';
        if (_this.ajaxType == 'update') {
          url = '/sd/work-style/update';
        }
        var params = {
          url: url,
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.addData.title = '';
            _this.addData.department = '';
            _this.addData.approvers = '';
            _this.addData.content = '';
            if (_this.editor) {
              _this.editor.setData('');
            }
            _this.addCurrentTableData = false;
          } else {
            _this.$Message.error('保存信息失败!');
          }
        }
      },
      // 显示修改界面
      update: function (id) {
        var _this = this;
        _this.ajaxType = 'update';
        _this.departmentArr = [];
        _this.approversArr = [];
        _this.getDepartmentArr();

        _this.id = id;
        var data = {
          id: _this.id,
        };
        var params = {
          url: '/sd/work-style/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];

            _this.addData.title = _data.title;
            _this.addData.department = _data.department ? _data.department.id : '';
            _this.addData.approvers = _data.approver ? _data.approver.id : '';
            _this.addData.content = _data.content;
            _this.status = _data.status ? _data.status.id : 1;
            _this.getApproversArr(_data.department.id);

            _this.showCurrentTableData = false;
            _this.addCurrentTableData = true;
            if (!_this.editor) {
              _this.initCkeditor();
            } else {
              _this.editor.setData(_this.addData.content);
            }
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      // 删除
      remove: function (id) {
        var _this = this;
        var selectionIds = [];
        selectionIds.push(id);
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            id: selectionIds,
          };
          var params = {
            url: '/sd/work-style/deleteMultiple',
            type: Util.method.get,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },

    }
  };
</script>
